﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using System.Globalization;
using System.IO;

namespace University.Workshop.WebApp
{
    public partial class Comments : System.Web.UI.Page
    {
        private static Topic topic = new Topic();
        protected void Page_Load(object sender, EventArgs e)
        {
            string topicId = Request["topicId"];
            
            
            topic.TopicId = Convert.ToInt64( topicId);
            topic = DataAccess.TopicDataAccess.SelectTopicById(topic.TopicId);
            Label1.Text = topic.Title;
            if (!IsPostBack)
            {
                try
                {
                    Collection<Comment> comments = DataAccess.CommentDataAccess.SelectComments(topic);
                    if (comments==null || comments.Count == 0)
                    {
                        LabelE.Text = "0 comments";
                    }
                    if (comments != null)
                        FillTable(comments);
                }
                catch (Exception er)
                {
                    LabelE.Text = er.Message;
                }
            }
            
        }

        private void FillTable(Collection<Comment> comments)
        {
            this.Table1.BorderWidth = 2;
            foreach (Comment currentComment in comments)
            {
                TableRow row = new TableRow();
                FillRow(currentComment, row,1);
                TableRow row2 = new TableRow();
                FillRow(currentComment, row2, 2);
            }
        }

        private void FillRow(Comment currentComment, TableRow row, int position)
        {
            TableCell[] cells = { new TableCell(), new TableCell() };
            row.Cells.AddRange(cells);
            if (position == 1)
            {
                User user = DataAccess.UserDataAccess.GetUserById(currentComment.UserId);
                row.Cells[0].Text = "User: " + user.ContactInformation.Email;
                row.Cells[1].Text = "Date: " + currentComment.Date.ToString();
                row.Cells[0].BorderStyle = BorderStyle.Double;
                row.Cells[0].Font.Bold=true;
                
                row.Cells[0].ForeColor = System.Drawing.Color.DarkBlue;
                row.Cells[1].ForeColor=System.Drawing.Color.BlueViolet;
                row.Cells[1].BorderStyle = BorderStyle.Solid;
            }
            else
            {
                Image image1 = new Image();
                if (Directory.Exists(Server.MapPath("~/Thumbnails/C") + currentComment.CommentId + "/"))
                {
                    DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/C") + currentComment.CommentId + "/");
                    FileInfo[] currentImages = dir.GetFiles();
                    string thumbUrl = "~/Thumbnails/C" + currentComment.CommentId + "/";
                    
                    foreach (FileInfo fi in currentImages)
                    {
                        if (String.IsNullOrEmpty(image1.ImageUrl))
                        {
                            image1.ImageUrl = thumbUrl + fi.Name;

                        }
                    }
                }
                else
                {
                    image1.ImageUrl = "~/Thumbnails/NoImage/noimage.jpg";
                }
                //currentComment.im
                row.Cells[0].Controls.Add(image1);
                row.Cells[1].Text = currentComment.Text;
            }
            row.BorderStyle = BorderStyle.Double;
            Table1.Rows.Add(row);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string topicId = Request["topicId"];
            Topic topic = DataAccess.TopicDataAccess.SelectTopicById(Convert.ToInt64(topicId, CultureInfo.CurrentCulture));
            
            Response.Redirect("RegisterComment.aspx?title="+topic.TopicId);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Topics.aspx?category="+topic.Category);
        }

    }
}